package cn.com.member.controller.advert;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.*;
import cn.com.api.mapper.ApiU_bonusMapper;
import cn.com.api.mapper.ApiU_templateMapper;
import cn.com.api.service.*;
import cn.com.util.CipherUtil;
import cn.com.util.DateUtil;
import cn.com.util.JsonUtil;
import cn.com.util.PageInfo;
import cn.com.util.Utils;

/**
 * 位置广告控制器
 */
@Controller
@RequestMapping
public class AdvertController {

	private static Logger logger = Logger.getLogger(AdvertController.class);
	@Autowired
	private ApiT_memberService apiT_memberService;
	@Autowired
	private ApiU_advertService apiU_advertService;
	@Autowired
	private ApiU_advert_positionService apiU_advert_positionService;
	@Autowired
	private ApiT_orderService apiT_orderService;
	@Autowired
	private ApiU_templateService apiU_templateService;

	/**
	 * 我的位置广告列表
	 * 
	 * @return
	 */
	@RequestMapping("advert_positionadvert-list")
	public ModelAndView advert_positionadvert_list(HttpServletRequest request, HttpServletResponse response,
			PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView("/advert/positionadvert/positionadvert-list");
		ApiTmember apiTmember = (ApiTmember) request.getSession().getAttribute("webuser");
		List<ApiU_advert> advertList = null;
		try {
			advertList = apiU_advertService.getMyAdvertInfos(apiTmember.getCid(), pageInfo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mv.addObject("advertList", advertList);
		return mv;
	}

	@RequestMapping("advert_positionadvertpage")
	public ModelAndView advert_positionadvertpage(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("/advert/positionadvert/positionadvert-page");
		return mv;
	}

	/**
	 * 跳转到位置广告添加或修改页面
	 * 
	 * @return
	 */
	@RequestMapping("advert_positionadvert-addOrUpdate")
	public ModelAndView advert_positionadvert_addOrUpdate(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("/advert/positionadvert/positionadvert-addorupdate");
		String positionid = request.getParameter("positionid");
		if (positionid != null && !"".equals(positionid)) {
			mv.addObject("positionid", positionid);
		}
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiU_advert u_advert = apiU_advertService.findU_advertById(Integer.valueOf(id));
			mv.addObject("u_advert", u_advert);
		}
		//
		// // 年龄区间字典
		// List<ApiT_enums> agerange =
		// apiT_enumsService.findT_enumssByItem_key("agerange");
		// List<ApiT_enums> ageranges =
		// apiT_enumsService.findT_enumssByParent_id(agerange.get(0).getId());
		// mv.addObject("ageranges", ageranges);

		// // 职业字典
		// List<ApiT_enums> profession =
		// apiT_enumsService.findT_enumssByItem_key("profession");
		// List<ApiT_enums> professions =
		// apiT_enumsService.findT_enumssByParent_id(profession.get(0).getId());
		// mv.addObject("professions", professions);

		// // 回去兴趣爱好标签库
		// List<U_label> alllabels = labelService.getAlllabels(1);
		// mv.addObject("alllabels", alllabels);

		// // H5模板
		// List<U_label> alllabels = labelService.getAlllabels(1);
		// mv.addObject("alllabels", alllabels);

		List<ApiU_template> templates = apiU_templateService.findTemplatesByType("4");
		mv.addObject("templates", templates);

		List<ApiU_advert_position> u_advert_positions = apiU_advert_positionService.findU_advert_positions();
		mv.addObject("u_advert_positions", u_advert_positions);

		return mv;
	}

	/**
	 * 创建广告，同时生成支付订单
	 * 
	 * @return
	 */
	@RequestMapping(value = "create_advert_positionadvert")
	public void create_advert_positionadvert(HttpServletRequest request, HttpServletResponse response,
			ApiU_advert u_advert) throws Exception {
		ApiTmember apiTmember = (ApiTmember) request.getSession().getAttribute("webuser");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = apiU_advertService.create_advert_positionadvert(apiTmember, u_advert);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 跳转到支付页面
	 **/
	@RequestMapping("advert_pay")
	public ModelAndView advert_pay(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("/order/orderPay");
		String return_url = request.getParameter("return_url");
		if (return_url == null)
			return_url = "";

		// 用户信息
		ApiTmember apiTmember = (ApiTmember) request.getSession().getAttribute("webuser");
		// 订单编号
		String rid = request.getParameter("rid");
		ApiT_order apiT_order = apiT_orderService.findT_orderById(Integer.valueOf(rid));
		ApiT_member apiT_member = apiT_memberService.findT_memberById(apiTmember.getCid());
		mv.addObject("order", apiT_order);
		mv.addObject("user", apiT_member);
		mv.addObject("return_url", return_url);

		return mv;
	}

	/**
	 * 广告未支付，跳转去支付页面
	 **/
	@RequestMapping("toadvertpay")
	public ModelAndView toadvertpay(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("/order/orderPay");
		String return_url = request.getParameter("return_url");

		// 用户信息
		ApiTmember apiTmember = (ApiTmember) request.getSession().getAttribute("webuser");
		// 广告id
		String advertid = request.getParameter("advertid");
		ApiT_order apiT_order = apiT_orderService.getorderInfoByadvertid(advertid);
		ApiT_member apiT_member = apiT_memberService.findT_memberById(apiTmember.getCid());
		ApiU_advert advert = apiU_advertService.findU_advertById(Integer.valueOf(advertid));
		if (return_url == null) {
			if (advert.getPosition_id() == 17) {// 喊个红包
				return_url = request.getContextPath() + "/hanbonus-list";
			} else if (advert.getPosition_id() == 3) {
				return_url = request.getContextPath() + "/bonus-list";
			}
		}
		mv.addObject("order", apiT_order);
		mv.addObject("user", apiT_member);
		mv.addObject("return_url", return_url);
		//
		return mv;
	}

	/**
	 * 广告位列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "advert_position-list")
	public ModelAndView advert_position_list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("/advert/positionadvert/positionlist");
		List<ApiU_advert_position> u_advert_positions = apiU_advert_positionService.findU_advert_positions();
		mv.addObject("advertpositions", u_advert_positions);
		return mv;
	}

	/**
	 * 删除广告
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_advert-delete")
	public void advert_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String advertid = request.getParameter("advertid");
			apiU_advertService.deleteAdvertInfo(Integer.valueOf(advertid));
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "操作失败");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	public static void main(String args[]) {
		String rid = Utils.getDingDanNo("");
		System.out.println(rid);
		System.out.println(Integer.valueOf(rid));
	}

}
